package org.example.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.common.R;
import org.example.interceptor.UserHolder;
import org.example.mapper.ApUserMapper;
import org.example.pojo.entity.ApComment;
import org.example.mapper.ApCommentMapper;
import org.example.pojo.entity.ApUser;
import org.example.pojo.vo.CommentVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.service.IApCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-26
 */
@Service
public class ApCommentServiceImpl extends ServiceImpl<ApCommentMapper, ApComment> implements IApCommentService {

    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 添加评论
     *
     * @param comment
     */
    @Override
    public void addComment(ApComment comment) {
        ApUser user = null;
        ApComment apCommentData = null;
        //回复功能,获取用户名称
        if (!StringUtils.isEmpty(comment.getReplyId())) {
            apCommentData = baseMapper.selectById(comment.getReplyId());
            user = apUserMapper.selectById(apCommentData.getUserId());
        }

        //添加评论
        ApComment apComment = new ApComment();
        apComment.setNewsId(comment.getNewsId());
        apComment.setUserId(UserHolder.get().getId());
        apComment.setCreateTime(LocalDateTime.now());
        if (org.apache.commons.lang3.StringUtils.isNoneBlank(comment.getReplyId())) {
            apComment.setComment(comment.getReplyComment());
            apComment.setReplyId(comment.getReplyId());
            apComment.setReplyName(user.getName());
            apComment.setReplyComment(apCommentData.getComment());
        } else {
            apComment.setComment(comment.getComment());
            apComment.setReplyId("0");

        }

        baseMapper.insert(apComment);
    }


    @Override
    public R getCommentList(String newsId, String commentId) {
        List<CommentVo> allCommentVo = baseMapper.getCommentList(newsId);
        //查找出某个评论的子数据
        List<CommentVo> commentList = allCommentVo.stream().filter(item -> item.getReplyId().equals(commentId)).collect(Collectors.toList());
        //获取二级及其以下
        List<CommentVo> twoListComment = allCommentVo.stream().filter(item -> !item.getReplyId().equals("0")).collect(Collectors.toList());
        //获取所有回复的数据一级
        List<CommentVo> childrenData = getChildrenData(commentList, twoListComment, new ArrayList<>());


        return R.ok(childrenData);
    }

    @Override
    public List<CommentVo> getCommentFirstList(String newsId) {
        List<CommentVo> commentVoList = new ArrayList<>();
        //获取评论列表
        List<ApComment> apComments = baseMapper.getCommentFirstList(newsId);

        for (ApComment apComment : apComments) {
            CommentVo commentVo = new CommentVo();

            //获取用户昵称
            ApUser user = apUserMapper.selectById(apComment.getUserId());
            commentVo.setName(user.getName());
            commentVo.setImage(user.getImage());
            BeanUtils.copyProperties(apComment, commentVo);

            QueryWrapper<ApComment> wrapper = new QueryWrapper();
            wrapper.eq("reply_id", apComment.getId());

            Long num = baseMapper.selectCount(wrapper);
            if (num > 0) {
                commentVo.setChildren(true);
            } else {
                commentVo.setChildren(false);
            }

            commentVoList.add(commentVo);
        }


        return commentVoList;

    }


    /**
     * 递归查询所有数据
     *
     * @param commentList
     * @param allCommentList
     * @return
     */
    private List<CommentVo> getChildrenData(List<CommentVo> commentList, List<CommentVo> allCommentList, List<CommentVo> voList) {

        for (CommentVo commentVo : commentList) {
            voList.add(commentVo);
            //查找子级数据
            List<CommentVo> childrenList = allCommentList.stream().filter(item -> item.getReplyId().equals(commentVo.getId().toString())).collect(Collectors.toList());
            this.getChildrenData(childrenList, allCommentList, voList);
        }

        return voList;
    }


}
